﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ListForms;

namespace ListsForm
{
    public partial class FrmProject : FrmBaseListForm
    {
        public FrmProject()
        {
            InitializeComponent();
        }

        public override void InitGridColumns()
        {
            DevExpress.XtraGrid.Columns.GridColumn colID = new DevExpress.XtraGrid.Columns.GridColumn();
            colID.Caption = "ID";
            colID.FieldName = "ProjectNo";
            colID.Name = "colID";
            colID.Visible = true;
            colID.Width = 100;
            colID.MinWidth = 100;
            colID.OptionsColumn.FixedWidth = true;
            colID.VisibleIndex = 0;

            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
            colName.Caption = "Name";
            colName.FieldName = "ProjectName";
            colName.Name = "colName";
            colName.Visible = true;
            colName.Width = 100;
            colName.MinWidth = 100;
            colName.OptionsColumn.FixedWidth = true;
            colName.VisibleIndex = 1;


            DevExpress.XtraEditors.Controls.LookUpColumnInfo colSoHieu = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("CompanyNo", "Company No", 20);
            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTenTK = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("CompanyName", "Company Name", 20);

            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repLookupTaiKhoan = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            repLookupTaiKhoan.Columns.Add(colSoHieu);
            repLookupTaiKhoan.Columns.Add(colTenTK);
            repLookupTaiKhoan.ValueMember = "CompanyID";
            repLookupTaiKhoan.DisplayMember = "CompanyName";
            repLookupTaiKhoan.DataSource = DB.Companies.ToList();

            DevExpress.XtraGrid.Columns.GridColumn colCompanyID = new DevExpress.XtraGrid.Columns.GridColumn();
            colCompanyID.Caption = "CompanyID";
            colCompanyID.FieldName = "CompanyID";
            colCompanyID.ColumnEdit = repLookupTaiKhoan;
            colCompanyID.Name = "colCompanyID";
            colCompanyID.Visible = true;
            colCompanyID.Width = 100;
            colCompanyID.MinWidth = 100;
            colCompanyID.OptionsColumn.FixedWidth = true;
            colCompanyID.VisibleIndex = 2;

            this.gridDSDoiTuong.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {
            repLookupTaiKhoan});

            this.gridViewDsDoiTuong.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
                colID, colName, colCompanyID});

            foreach (DevExpress.XtraGrid.Columns.GridColumn gc in gridViewDsDoiTuong.Columns)
            {
                gc.FilterMode = DevExpress.XtraGrid.ColumnFilterMode.DisplayText;
                gc.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
            }
        }

        public override void LoadData()
        {
            try
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Projects);
                gridDSDoiTuong.DataSource = DB.Projects.ToList();
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        public override void CreateNew()
        {
            try
            {
                FrmEditProject frm = new FrmEditProject(null);
                frm.RequestChangeInstance += new FrmBaseEditForm.OnRequestChangeInstance(LoadData);
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                //BasicForms.FrmWarning.Show(ex.Message);
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        protected override void Edit()
        {
            try
            {
                int[] tmp = gridViewDsDoiTuong.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    Lists.Project project = (Lists.Project)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (project != null)
                    {
                        FrmEditProject frm = new FrmEditProject(project);
                        frm.RequestChangeInstance += new FrmBaseEditForm.OnRequestChangeInstance(LoadData);
                        frm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        protected override void Delete()
        {
            try
            {
                int[] selectedIndexes = gridViewDsDoiTuong.GetSelectedRows();

                if (selectedIndexes != null && selectedIndexes.Length > 0)
                {
                    int selectedRowIndex = selectedIndexes[0];
                    Lists.Project deletedObj = (Lists.Project)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (deletedObj != null)
                    {
                        if (MessageBox.Show("Are you sure delete " + "'" + deletedObj.ProjectName + "'" + "?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.No)
                            return;
                        else
                        {
                            DB.Projects.DeleteOnSubmit(deletedObj);
                            DB.SubmitChanges();
                            LoadData();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show("Cannot delete this project!");
            }
        }
    }
}
